/*
 * Copyright (c) 2009 Sun Microsystems, Inc.
 * Copyright (c) 2011 Oracle Corporation.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to
 * deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 **/

package com.sun.spot.wot.gateway;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
import com.sun.spot.wot.http.HttpRequest;
import com.sun.spot.wot.http.HttpResponse;

/**
 *
 * @author vgupta
 */
public class ResourceCache {
    HashMap cache = new HashMap();

    public synchronized boolean insert(HttpRequest request, HttpResponse response) {
        // XXX Automatically assign an Age tag ???
        if (!request.getMethod().equalsIgnoreCase("GET") ||
                request.getQueryString() != null) {
            System.err.println("Did not insert request, response in cache!");
            return false;
        }
        
        ResourceCacheEntry rce = new ResourceCacheEntry(request, response);

        cache.put(request.getPathInfo(), rce);
        //System.out.println("Inserting into resource cache:\n" + rce);
        return true;
    }

    public synchronized HttpResponse getCachedResponse(HttpRequest request) {
        HttpResponse result = null;

        if (!request.getMethod().equalsIgnoreCase("GET") ||
                request.getQueryString() != null) {
            System.err.println("Warning: Cannot use resource cache " +
                    "for this request");
            return result;
        }

        ResourceCacheEntry rce = (ResourceCacheEntry) cache.get(request.getPathInfo());
        if (rce != null) {
            if (rce.isExpired()) {
//                System.out.println("Resource Cache Entry has expired");
                cache.remove(request.getPathInfo());
            } else {
                result = rce.getAcceptableResponse(request);
            }
        }

        return result;
    }

    public synchronized String getSummary() {
        StringBuffer sb = new StringBuffer();
        int size = cache.size();

        if (size == 0) {
            sb.append("empty");
        } else {
            sb.append(size + " entries");
        }
        
        return sb.toString();
    }

    public synchronized String toString() {
        StringBuffer sb = new StringBuffer();
        Set keys = cache.keySet();
        Iterator i = keys.iterator();
        
        while (i.hasNext()) {
            ResourceCacheEntry rce = (ResourceCacheEntry) cache.get(i.next());
            sb.append(rce.toString());
        }

        return sb.toString();
    }
}
